﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using ncp.Business;
using ncp.Business.Common;

namespace ncp.Web.member
{
    public partial class shop_product : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var userInfo = WebMasterCookie.GetCookie();
            if (userInfo == null)
            {
                Response.Redirect("/login.aspx");
            }
            AjaxPro.Utility.RegisterTypeForAjax(typeof(shop_product));
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetProductList(int pageIndex, int pageSize, string sorting)
        {
            var userInfo = WebMasterCookie.GetCookie();
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            if (userInfo != null)
            {
                var dicWhere = new Dictionary<string, object>();
                dicWhere.Add("Client_Id", userInfo.Id);
                var dataTable = ProductInfoBll.GetProductInfoTable(pageIndex, pageSize, "*", sorting + " desc", dicWhere,  CommonConst.DB_Name);
                StringBuilder builder = new StringBuilder();
                foreach (DataRow row in dataTable.Rows)
                {
                    var productId = EncryptHelper.EncryptString(row["Product_Id"].ToString());
                    var title = row["Product_Title"];
                    var cuttitle = CommonBll.CutString(title.ToString(), 70, true);
                    builder.Append("<ul>");
                    builder.AppendFormat("<li class=\"title\"><a href=\"/member/product_add/{0}.html\" title=\"{1}\">{2}</a></li>", productId, title,cuttitle);
                    builder.Append("<li class=\"click\"></li>");
                    builder.AppendFormat("<li class=\"datetime\">{0}</li>", Convert.ToDateTime(row["Product_StartTime"]).ToString("yyyy-MM-dd HH:mm"));
                    var audit = int.Parse(row["Product_Status"].ToString()) == 0
                                    ? "<span style=\"color:red;\">未通过</span>"
                                    : "<span style=\"color:green;\">已通过</span>";
                    builder.AppendFormat("<li class=\"audit\">{0}</li>", audit);
                    builder.AppendFormat("<li class=\"operate\"><a href=\"/member/product_add/{0}.html\">修改</a>&nbsp;<a href=\"javascript:void(0);\" onclick=\"onDelete('{0}')\">删除</a>", productId);
                    builder.AppendFormat("&nbsp;<a target=\"_blank\" href=\"/product/preview/{0}.html\">预览</a>", productId);
                    builder.Append("</li>");
                    builder.Append("</ul>");
                }
                jsonResult.Add("isreload", true);
                jsonResult.Add("msg",builder.ToString());
            }
            else
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", "登录超时，请退出重新登录！");
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetProductCount()
        {
            var userInfo = WebMasterCookie.GetCookie();
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            if (userInfo != null)
            {
                var count = ProductInfoBll.GetProductInfoCount(" Client_Id=" + userInfo.Id,  CommonConst.DB_Name);
                jsonResult.Add("isreload", true);
                jsonResult.Add("msg", count);
            }
            else
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", "登录超时，请退出重新登录！");
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //判断用户积分
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetProductLimit()
        {
            var userInfo = WebMasterCookie.GetCookie();
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            if (userInfo != null)
            {
                var dt = ClientInfoBll.GetClientById(userInfo.Id,  CommonConst.DB_Name);
                if (dt.Rows.Count > 0)
                {
                    //判断当前用户的产品发布条数
                    var points = int.Parse(dt.Rows[0]["User_Points"].ToString());
                    if (points >CommonConst.gLosePoints)
                    {
                        jsonResult.Add("isreload", true);
                        jsonResult.Add("msg", "");
                    }
                    else
                    {
                        jsonResult.Add("isreload", false);
                        jsonResult.Add("msg", "积分不够，不能继续添加产品!");
                    }
                }
                else
                {
                    jsonResult.Add("isreload", false);
                    jsonResult.Add("msg", "登录超时，请退出重新登录!");
                }
            }
            else
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", "登录超时，请退出重新登录！");
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string Delete(string id)
        {
            var userInfo = WebMasterCookie.GetCookie();
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            if (userInfo != null)
            {
                try
                {
                    ProductInfoBll.DeleteProductInfo(EncryptHelper.DecryptString(id), userInfo.Id,  CommonConst.DB_Name);
                    Dictionary<string, object> dicHandleLog = new Dictionary<string, object>();
                    dicHandleLog.Add("user_id", userInfo.Id);
                    dicHandleLog.Add("computername", userInfo.Computername);
                    dicHandleLog.Add("computerip", userInfo.Computerip);
                    dicHandleLog.Add("handle_note", "删除产品");
                    dicHandleLog.Add("obj_guid", "Product_Id=" + id);
                    LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);

                    jsonResult.Add("isreload", true);
                    jsonResult.Add("msg", "删除成功！");
                }
                catch (Exception ex)
                {
                    jsonResult.Clear();
                    jsonResult.Add("isreload", false);
                    jsonResult.Add("msg", ex.Message);
                }

            }
            else
            {
                jsonResult.Clear();
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", "登录超时，请退出重新登录！");
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }
    }
}
